<?php
/**
 * Created by PhpStorm.
 * User: yunpeng
 * Date: 2019/1/4
 * Time: 10:34
 */

namespace App\Admin\Controllers;

use App\Models\User;
use App\Models\Cash;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class CashController  extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('提现列表');
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('编辑banner');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('创建banner');
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Cash::class, function (Grid $grid) {
            // 使用 with 来预加载商品类目数据，减少 SQL 查询
            $grid->model()->with(['user']);
            $grid->filter(function($filter){

                // 去掉默认的id过滤器
                $filter->disableIdFilter();

                // 在这里添加字段过滤器
                $filter->like('user.name', '姓名');
                $filter->equal('status')->radio([
                    ''   => 'All',
                    0    => '申请中',
                    1    => '已打款',
                    2    => '不通过',
                ]);

            });
            $grid->id('ID')->sortable();
            $grid->user_id('user_id');
            // Laravel-Admin 支持用符号 . 来展示关联关系的字段
            $grid->column('user.name', '姓名');
            $grid->money('提现金额');
            $grid->status('状态')->using(['0'=>'申请中','1'=>'已打款','2'=>'不通过']);
            $grid->disableCreateButton();
            $grid->disableExport();
            //禁用批量删除按钮
            $grid->tools(function ($tools) {
                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                });
            });
            //隐藏操作
            $grid->actions(function ($actions) {
                $actions->disableDelete();
                $actions->disableView();;
            });

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        // 创建一个表单
        return Admin::form(Cash::class, function (Form $form) {
            $form->radio('status', '状态')->options(['0' => '申请中', '1' => '已打款','2'=>'不通过'])->default('0');
            // 创建一个富文本编辑器
            $form->editor('reason', '原因');
        });
    }
}

